#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include <cmath>

#include "structure.h"
#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "draw.h"

static int press_flag = 0;
static int mouse_x = 0, mouse_y = 0;
static int mouse_x_press = 0, mouse_y_press = 0;
static int screen_offset_x_press = 0, screen_offset_y_press = 0;
static double screen_resolution_press = 0;
static GuiInterface *_gui;

// 以降GTK関連

void
initialize_for_gtk(void)
{
  XInitThreads();
  g_thread_init(NULL);
  gdk_threads_init();

  gtk_set_locale ();
  //gtk_init (&argc, &argv);
  gtk_init (NULL, NULL);
  gdk_rgb_init();
}

void
initialize_for_gui(GuiInterface *gui)
{
  _gui = gui;
}

GThreadFunc
gtk_main_thread(gpointer data)
{
  //gdk_threads_enter();
  gtk_main ();
  //gdk_threads_leave();
  return NULL;
}

void
on_window1_destroy                     (GtkObject       *object,
                                        gpointer         user_data)
{
  gtk_main_quit();
}


gboolean
on_drawingarea1_key_press_event        (GtkWidget       *widget,
                                        GdkEventKey     *event,
                                        gpointer         user_data)
{
  //fprintf(stderr,"key_press\n");
  return FALSE;
}


gboolean
on_drawingarea1_button_press_event     (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{
  if(event->button == 1){
    // 左クリック
    press_flag = 1;
  }else if(event->button == 2){
    // 真ん中クリック
    press_flag = 2;
    screen_resolution_press = _gui->screen_resolution;
  }else if(event->button == 3){
    // 右クリック
    press_flag = 3;
    screen_offset_x_press = _gui->screen_offset_x;
    screen_offset_y_press = _gui->screen_offset_y;
  };

  mouse_x_press = mouse_x;
  mouse_y_press = mouse_y;
  //fprintf(stderr, "press\n");
  return FALSE;
}


gboolean
on_drawingarea1_button_release_event   (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{
  press_flag = 0;
  //fprintf(stderr, "button_release\n");
  return FALSE;
}


gboolean
on_drawingarea1_motion_notify_event    (GtkWidget       *widget,
                                        GdkEventMotion  *event,
                                        gpointer         user_data)
{
  double width = widget->allocation.width;   /// ウィンドウの幅
  double height = widget->allocation.height; /// ウィンドウの高さ

  /// マウスポインタ座標を受け取る
  mouse_x = (int)event->x;
  mouse_y = (int)event->y;

  // 地図並進：描画エリアを右クリック
  if(press_flag==3){
    _gui->screen_offset_x = screen_offset_x_press - (mouse_x_press-mouse_x);
    _gui->screen_offset_y = screen_offset_y_press - (mouse_y_press-mouse_y);
  }

  // 地図ズーム：描画エリアを真ん中クリック
  if(press_flag==2){
    double resolution = screen_resolution_press + (double)(mouse_y-mouse_y_press)/height/2;
    //_gui->screen_resolution = screen_resolution_press + (double)(mouse_y_press-mouse_y)/height;
    if(resolution < 0.001) resolution = 0.001; // 1mm
    _gui->screen_resolution = resolution;
  }

  //fprintf(stderr, "motion_notify\n");
  return FALSE;
}


gboolean
on_drawingarea1_expose_event           (GtkWidget       *widget,
                                        GdkEventExpose  *event,
                                        gpointer         user_data)
{
  GdkPixmap *pixmap1 = (GdkPixmap*)lookup_widget(widget, "pixmap1");

  if(pixmap1)
    gdk_draw_pixmap(widget->window, widget->style->black_gc,
		    pixmap1,
		    event->area.x, event->area.y,
		    event->area.x, event->area.y,
		    event->area.width,
		    event->area.height);

  return FALSE;
}


gboolean
on_drawingarea1_configure_event        (GtkWidget       *widget,
                                        GdkEventConfigure *event,
                                        gpointer         user_data)
{
  GtkWidget *window1 = (GtkWidget*)lookup_widget(widget, "window1");
  GdkPixmap *pixmap1 = (GdkPixmap*)lookup_widget(widget, "pixmap1");

  if(pixmap1) gdk_pixmap_unref(pixmap1);
  pixmap1 = gdk_pixmap_new(widget->window,
			   widget->allocation.width,
			   widget->allocation.height,
			   -1);
  gdk_draw_rectangle(pixmap1, widget->style->white_gc,
                     TRUE, 0, 0, 
		     widget->allocation.width,
		     widget->allocation.height);
  g_object_set_data(G_OBJECT (window1), "pixmap1", pixmap1);

  
  return FALSE;
}

